package com.leyou.item.inter.fallback;

import com.leyou.common.bean.Result;
import com.leyou.common.bean.ResultCode;
import com.leyou.item.inter.api.ICategoryService;
import com.leyou.item.inter.pojo.Category;
import com.netflix.hystrix.exception.HystrixTimeoutException;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zqq
 * @ClassName CategoryServiceFallbackFactory
 * @Description
 * @date 2020/3/4-10:09
 */
@Component
@Slf4j
public class CategoryServiceFallbackFactory implements FallbackFactory<ICategoryService> {
    @Override
    public ICategoryService create(Throwable throwable) {
        return new ICategoryService(){

            @Override
            public Result<List<Category>> queryCategoriesByPid(Long pid) {
                log.error("fallback,causedby: " + throwable);
                if (throwable != null) {
                    if (throwable instanceof HystrixTimeoutException) {
                        return Result.newFailure(ResultCode.S_REQUEST_TIME_OUT);
                    }
                }
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }

            @Override
            public Result<List<Category>> queryByBrandId(Long bid) {
                log.error("fallback,causedby: " + throwable);
                if (throwable != null) {
                    if (throwable instanceof HystrixTimeoutException) {
                        return Result.newFailure(ResultCode.S_REQUEST_TIME_OUT);
                    }
                }
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }

            @Override
            public Result<List<String>> queryNamesByIds(List<Long> ids) {
                log.error("fallback,causedby: " + throwable);
                if (throwable != null) {
                    if (throwable instanceof HystrixTimeoutException) {
                        return Result.newFailure(ResultCode.S_REQUEST_TIME_OUT);
                    }
                }
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }

            @Override
            public Result<Category> queryCategoryById(Long id) {
                log.error("fallback,causedby: " + throwable);
                if (throwable != null) {
                    if (throwable instanceof HystrixTimeoutException) {
                        return Result.newFailure(ResultCode.S_REQUEST_TIME_OUT);
                    }
                }
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }
        };
    }
}
